import {createWebHashHistory, createRouter} from "vue-router";
import map from "../components/map"
// import {createMemoryHistory} from "vue-router";
// 1. 定义路由组件.
// 也可以从其他文件导入
//导入登录和注册组件
import login from '@/router/loginAndRegister/login'
import register from '@/router/loginAndRegister/register'
import loginView from '@/router/loginAndRegister/loginView'
import registerView from '@/router/loginAndRegister/registerView'
import findPassword from '@/router/loginAndRegister/findPassword'

//导入审核员组件
import auditorView from "@/router/auditor/auditorView";

//导入管理员组件
import administratorView from "@/router/administrator/administratorView";
import roads from "@/router/administrator/updateRoad/roads";
import roadNodes from "@/router/administrator/updateRoad/roadNodes";
import auditorManagement from "@/router/administrator/auditorManage/auditorManagement";
import driverManagement from "@/router/administrator/driverManage/driverManagement";
import admInform from "@/router/administrator/admInform";
import masterhomePage from "@/router/administrator/masterPersonal/masterhomePage";
import masterPersonal1 from "@/router/administrator/masterPersonal/masterPersonal1"
import masterPassword from "@/router/administrator/masterPersonal/masterPassword"
import blockedRoads from "@/router/administrator/updateRoad/blockedRoads";
import expressways from "@/router/administrator/updateRoad/expressways";
import roadStones from "@/router/administrator/updateRoad/roadStones";
//导入司机组件
import driverView from "@/router/driver/driverView";
// import homePage from "@/router/administrator/masterPersonal/homePage"
// 2. 定义一些路由
// 每个路由都需要映射到一个组件。
// 我们后面再讨论嵌套路由。
const routes = [
    {path: '/',redirect:'/login'},//重定位
    {
        path: "/map",
        component: map
    },


    //登录和注册组件
    {
        path: '/login',
        component: loginView,
        children: [{
            path: '/login',
            component: login}]
    },
    {
        path: '/register',
        component: registerView,
        children: [
            {path: '/register',component: register},
            {path: '/register/findPassword',component: findPassword}
        ]
    },
    //审核员组件
    {   path: '/auditor',
        component: auditorView,
        beforeEnter:function(to, from, next) {
            if (sessionStorage.getItem("user")) {
                if(JSON.parse(sessionStorage.getItem("user")).userType==='管理员'){
                    next('/administrator')
                }else if(JSON.parse(sessionStorage.getItem("user")).userType==='司机'){
                    next('/driver')
                }else if(JSON.parse(sessionStorage.getItem("user")).userType==='审核员'){
                    next()
                }
            } else {
                alert("请先登录");
                next("/login");
            }
        },
    },
    //管理员组件
    {
        path: '/administrator',
        component: administratorView,
        beforeEnter:function(to, from, next) {
            if (sessionStorage.getItem("user")) {
                if(JSON.parse(sessionStorage.getItem("user")).userType==='管理员'){
                    next()
                }else if(JSON.parse(sessionStorage.getItem("user")).userType==='司机'){
                    next('/driver')
                }else if(JSON.parse(sessionStorage.getItem("user")).userType==='审核员'){
                    next('/auditor')
                }
            } else {
                alert("请先登录");
                next("/login");
            }
        },
        children: [
            {path: '/administrator/updateRoad/roads',component: roads},
            {path: '/administrator/updateRoad/roadNodes',component: roadNodes},
            {path: '/administrator/auditorManagement',component: auditorManagement},
            {path: '/administrator/driverManagement',component: driverManagement},
            {path: '/administrator/admInform',component: admInform},
            {path: '/administrator',component: masterhomePage},
            {path: '/administrator/masterPersonal/masterPersonal1',component: masterPersonal1},
            {path: '/administrator/masterPersonal/masterPassword',component: masterPassword},
            {path: '/administrator/updateRoad/roadStones',component: roadStones},
            {path: '/administrator/updateRoad/blockedRoads',component: blockedRoads},
            {path: '/administrator/updateRoad/expressways',component: expressways},
            {path: '/administrator/updateRoad/driverManagement',component: driverManagement}
        ]
    },
    //司机组件
    {
        path: '/driver',
        component: driverView,
        beforeEnter:function(to, from, next) {
            if (sessionStorage.getItem("user")) {
                if(JSON.parse(sessionStorage.getItem("user")).userType==='管理员'){
                    next('/administrator')
                }else if(JSON.parse(sessionStorage.getItem("user")).userType==='司机'){
                    next()
                }else if(JSON.parse(sessionStorage.getItem("user")).userType==='审核员'){
                    next('/auditor')
                }
            } else {
                alert("请先登录");
                next("/login");
            }
        },
    }
]

// 3. 创建路由实例并传递 `routes` 配置
// 你可以在这里输入更多的配置，但我们在这里
// 暂时保持简单
const router = createRouter({
    // 4. 内部提供了 history 模式的实现。为了简单起见，我们在这里使用 hash 模式。
    history: createWebHashHistory(),//隐藏路由,

    //nginx配置
    base: process.env.BASE_URL,

    routes, // `routes: routes` 的缩写
})
//路由守卫
export default router
